package com.qipay.sp.excutor.wx;

import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.SignSortMD5Utils;
import com.qipay.utils.XmlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class WxWapPayExcutor extends AbstractExcutor {

    private final static String KEY_NAME="key";
    private final static String CONFIG_KEY_NAME="key";
    private final static String CONFIG_MCHID_NAME="mch_id";
    private final static String CONFIG_SUBMCHID_NAME="sub_mch_id";
    private final static String CONFIG_APPID_NAME="appid";
    private final static String SIGN_NAME="sign";
    private final static String API_PARAM_MCHID="mch_id";
    private final static String API_PARAM_SUBMCHID="sub_mch_id";
    private final static String API_PARAM_APPID="appid";
    private final static String API_PARAM_OUT_TRADE_NO="out_trade_no";
    private final static String API_PARAM_TOTAL_FEE="total_fee";
    private final static String API_PARAM_BODY="body";
    private final static String API_PARAM_NONCE_STR="nonce_str";
    private final static String API_PARAM_SPBILL_CREATE_IP="spbill_create_ip";
    private final static String API_URL="https://api.mch.weixin.qq.com/pay/unifiedorder";

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.WX_WAP_PAY;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put(WxWapPayExcutor.API_PARAM_OUT_TRADE_NO, order.getTradeNo());
        apiParams.put(WxWapPayExcutor.API_PARAM_BODY, order.getBody());
        apiParams.put(WxWapPayExcutor.API_PARAM_TOTAL_FEE, order.getAmount());
        apiParams.put(WxWapPayExcutor.API_PARAM_NONCE_STR, new Date().getTime());
        apiParams.put(WxWapPayExcutor.API_PARAM_SPBILL_CREATE_IP,extendOrderParams.get("mch_create_ip"));

        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        Object key = getVal(spconfig, WxWapPayExcutor.CONFIG_KEY_NAME);
        Object mchId = getVal(spconfig, WxWapPayExcutor.CONFIG_MCHID_NAME);
        Object appId = getVal(spconfig, WxWapPayExcutor.CONFIG_APPID_NAME);
        Object subMchId = getVal(spconfig, WxWapPayExcutor.CONFIG_SUBMCHID_NAME);

        params.put(WxWapPayExcutor.API_PARAM_MCHID, mchId);
        params.put(WxWapPayExcutor.API_PARAM_APPID, appId);
        params.put(WxWapPayExcutor.API_PARAM_SUBMCHID, subMchId);

        String sign = SignSortMD5Utils.getSign(params,
                key.toString(),
                WxWapPayExcutor.SIGN_NAME,
                WxWapPayExcutor.KEY_NAME);

        params.put(WxWapPayExcutor.SIGN_NAME, sign);

        String result = sendPost(params, WxWapPayExcutor.API_URL);
        if(StringUtils.isBlank(result)){
            throw new Exception("请求微信WAP接口失败");
        }
        Map<String, Object> responseMap = XmlUtils.toMap(result);
        Map<String, Object> resultMap = new HashMap<>();

        if(responseMap == null || responseMap.get("return_code") == null || !responseMap.get("return_code").toString().equals("SUCCESS"))
        {
            logger.error("请求微信WAP接口失败:" + result);
            return null;
        }
        if(!isResponseValid(responseMap, key.toString(), WxWapPayExcutor.SIGN_NAME,
                WxWapPayExcutor.KEY_NAME)) {
            logger.error("请求微信WAP返回值的签名失败:" + result);
            return null;
        }

        if(responseMap.get("result_code") == null || !responseMap.get("result_code").toString().equals("SUCCESS"))
        {
            logger.error("请求微信WAP接口业务执行失败:" + result);
            resultMap.put("err_code", responseMap.get("err_code"));
            resultMap.put("err_msg", responseMap.get("err_msg"));
            return resultMap;
        }

        resultMap.put("result_code", "0");
        String payInfo = StringUtils.substringAfter(responseMap.get("mweb_url").toString(),"?");
        payInfo = "http://api.huobapay.com/api/redirect/v1/wx/wap" + "?" + payInfo;
        resultMap.put("pay_info", payInfo);
        return resultMap;
    }
}
